* Connector Paper Figures

clear all
set more off, perm
cap log close
cd "/disk/eagedisk1/mahc_sl.work/stiebris/code_connector/"
global zip "../../Master_Formatted_Data/zip2stablewrap.dta"
global zip_income "../raw/MedianZIP-3.xlsx"
global coord "../raw/tl_2016_us_zcta510.shp"

* Programs to Install
ssc install matsave

program main
* Main Figures
	persistence_difference
	het_effects
	rd_enroll
	region_netpremiums
	enrollment_source
end

program persistence_difference
forv zp = 0/1 {
	use ../output/persistence_months, clear
	
forval i = -1/12 {
	local k = `i'+1
	
reg enr_`k'months_postletter i.arm i.batch if use_id_90==1 & new_elig==`zp', r

di "enrolled `i' days"
matrix temp = r(table)

matrix zp`zp'_difference_nocontrols = nullmat(zp`zp'_difference_nocontrols)\(temp[1,2], temp[1,3], temp[1,4])

}

esttab matrix(zp`zp'_difference_nocontrols)
matsave zp`zp'_difference_nocontrols, replace saving


use zp`zp'_difference_nocontrols, clear

gen days = . 

. replace days = -30 if _n==1

. replace days = days[_n-1]+30 if _n > 1


rename c1 arm2
rename c2 arm3
rename c3 arm4

* Offset arms slightly
gen orig_n = _n
expand 3
sort orig_n

gen arm = mod(_n-1,3)+2
forv a=2/4 {
	replace arm`a'=. if arm!=`a'
}

replace days = days - 2 if arm==2
replace days = days + 2 if arm==4

label var arm2 "Generic reminder"
label var arm3 "Personalized reminder"
label var arm4 "Streamlined enrollment"
label var days "# of Days Since Letter Mailed"

set scheme s1color
graph set eps fontface "CMU Serif"
graph set ps fontface "CMU Serif"
graph set svg fontface "CMU Serif"
graph set window fontface "CMU Serif"

if `zp'==0 local x = 4
if `zp'==1 local x = 1
if `zp'==0 local y = 0.032
if `zp'==1 local y = 0.065

* Arm Differences
twoway connected arm2 arm3 arm4 days, ///
lcolor(black black black) msymbol(s t c) ///
msize(*1.2 *1.2 *1.2) mlcolor(black black black) ///
mfcolor("122 81 149" "239 86 117" "255 166 0") ///
plotregion(lstyle(none)) xtitle("Days Since Letter Mailed", margin(top) size(*1.2)) ///
xscale(noextend) yscale(nofextend) xlabel(-30(30)360,) ylabel(, angle(0) format(%9.3f) nogrid) legend(order(3 2 1) ring(0) position(`x') cols(1)) graphregion(color(white)) xline(0, lcolor(black)) text( ///
`y' -33 "Effect on" "Enrollment", ///
placement(se) justification(left) size(*1) box margin(top_bottom) fcolor(white) lcolor(white)) yline(0, lcolor(black))

graph export ../output/main_results/figures/zp`zp'_difference_nocontrols.pdf, replace
}
end

program het_effects
local folder "asof_Dec2020"
local population "fullsample overmed undermed zeroprem_elig zeroprem_inelig spanish english duesooner duelater"

local fullsample "use_id_90==1"
local zeroprem_elig "use_id_90==1 & new_elig==1"
local zeroprem_inelig "use_id_90==1 & new_elig==0"
local spanish "use_id_90==1 & spanish_letter==1"
local english "use_id_90==1 & spanish_letter==0"
local undermed "use_id_90==1 & elig_age <= med_age & elig_age!=."
local overmed "use_id_90==1 & elig_age > med_age & elig_age!=."
local duesooner "use_id_90==1 & time_to_formdue < 30" 
local duelater "use_id_90==1 & time_to_formdue > 30 & time_to_formdue!=."

foreach sample of local population{
use ../output/connector_regression_prep, clear
egen med_age = median(elig_age)
encode zipstring, gen(zip_factor)

if "`sample'" == "fullsample" local col = "B"
if "`sample'" == "zeroprem_elig" local col = "C"
if "`sample'" == "zeroprem_inelig" local col = "D"
if "`sample'" == "overmed" local col = "E"
if "`sample'" == "undermed" local col = "F"
if "`sample'" == "spanish" local col = "G"
if "`sample'" == "english" local col = "H"
if "`sample'" == "duesooner" local col = "I"
if "`sample'" == "duelater" local col = "J"

di "Outcome: `var', no controls, sample: `sample'" 
rename arm4 arm4_`sample'

reg enroll90 arm2 arm3 arm4_`sample' i.batch if ``sample'', r
est sto `col'1
reg enroll90 arm1 arm2 arm4_`sample' i.batch if ``sample'', r
est sto `col'2
}

set scheme s2color

graph set eps fontface "CMU Serif"
graph set ps fontface "CMU Serif"
graph set svg fontface "CMU Serif"
graph set window fontface "CMU Serif"

* Arm 4 vs. Arm 1
coefplot (B1, lcolor(black) mfcolor(gs13) ciopts(lcolor(black))) ///
(C1, lcolor(black) mfcolor(gs13) ciopts(lcolor(black))) ///
(D1, lcolor(black) mfcolor(gs13) ciopts(lcolor(black))) ///
(E1, lcolor(black) mfcolor(gs13) ciopts(lcolor(black))) ///
(F1, lcolor(black) mfcolor(gs13) ciopts(lcolor(black))) ///
(G1, lcolor(black) mfcolor(gs13) ciopts(lcolor(black))) ///
(H1, lcolor(black) mfcolor(gs13) ciopts(lcolor(black))) ///
(I1, lcolor(black) mfcolor(gs13) ciopts(lcolor(black))) ///
(J1, lcolor(black) mfcolor(gs13) ciopts(lcolor(black))), keep(arm4_*) xline(0, lcolor(black)) ///
graphregion(color(white)) bgcolor(white) ylabel(, nogrid) ///
coeflabels(arm4_fullsample = "Full Sample" arm4_zeroprem_elig = "Zero-premium eligible" arm4_zeroprem_inelig = "Zero-premium ineligible" arm4_overmed = "Above median age" arm4_undermed = "Below median age" arm4_spanish = "Spanish letter" arm4_english = "English letter" arm4_duesooner = "Enrollment deadline sooner" arm4_duelater = "Enrollment deadline later", labcolor(black)) legend(off) pstyle(p5) lcolor(black) mlabel mlabcolor(black) format(%12.3f) mlabposition(1) mlabgap(*2) grid(none) xtitle(" " "Effect on Enrollment", size(*1.2))
graph export "../output/main_results/figures/asof_Dec2020/het_effects_coefplot_4v1.pdf", replace

* Arm 4 vs. Arm 3
coefplot (B2, lcolor(black) mfcolor(gs13) ciopts(lcolor(black))) ///
(C2, lcolor(black) mfcolor(gs13) ciopts(lcolor(black))) ///
(D2, lcolor(black) mfcolor(gs13) ciopts(lcolor(black))) ///
(E2, lcolor(black) mfcolor(gs13) ciopts(lcolor(black))) ///
(F2, lcolor(black) mfcolor(gs13) ciopts(lcolor(black))) ///
(G2, lcolor(black) mfcolor(gs13) ciopts(lcolor(black))) ///
(H2, lcolor(black) mfcolor(gs13) ciopts(lcolor(black))) ///
(I2, lcolor(black) mfcolor(gs13) ciopts(lcolor(black))) ///
(J2, lcolor(black) mfcolor(gs13) ciopts(lcolor(black))), keep(arm4_*) xline(0, lcolor(black)) ///
graphregion(color(white)) bgcolor(white) ylabel(, nogrid) ///
coeflabels(arm4_fullsample = "Full Sample" arm4_zeroprem_elig = "Zero-premium eligible" arm4_zeroprem_inelig = "Zero-premium ineligible" arm4_overmed = "Above median age" arm4_undermed = "Below median age" arm4_spanish = "Spanish letter" arm4_english = "English letter" arm4_duesooner = "Enrollment dealine sooner" arm4_duelater = "Enrollment deadline later", labcolor(black)) legend(off) pstyle(p5) lcolor(black) mlabel mlabcolor(black) format(%12.3f) mlabposition(1) mlabgap(*2) grid(none) xtitle(" " "Effect on Enrollment", size(*1.2))
graph export "../output/main_results/figures/asof_Dec2020/het_effects_coefplot_4v3.pdf", replace
end

program rd_enroll
* Premium and Enrollment
use ../output/rd_setup_first_enroll, clear

preserve
collapse (sum) n (mean) bin_2 bin_5 FamilyLevelBalancePremiumAmt, by(bin_1)
collapse (mean) n FamilyLevelBalancePremiumAmt, by(bin_5)
rename FamilyLevelBalancePremiumAmt premium

set scheme s1color
graph set eps fontface "CMU Serif"
graph set ps fontface "CMU Serif"
graph set svg fontface "CMU Serif"
graph set window fontface "CMU Serif"

* # Enrollees RD Graph
twoway (scatter n bin_5, mcolor(gs8))(lfit n bin_5 if bin_5>=150 & bin_5<200, lcolor(black))(lfit n bin_5 if bin_5>=200 & bin_5<250, lcolor(black)) (lfit n bin_5 if bin_5>=250 & bin_5<300, lcolor(black)) (lfit n bin_5 if bin_5>=300 & bin_5<350, lcolor(black)) (lfit n bin_5 if bin_5>=350 & bin_5<400, lcolor(black)), xline(150, lcolor(gs6)) xline(200, lcolor(gs6)) xline(250, lcolor(gs6)) xline(300, lcolor(gs6)) xline(350, lcolor(gs6)) xline(138, lcolor(gs6) lpattern(dash)) xlabel(138 "*" 150 "150" 200 "200" 250 "250" 300 "300" 350 "350") ///
graphregion(color(white)) plotregion(lstyle(none)) xscale(nofextend) yscale(nofextend) ylab(, angle(0)) ///
xtitle("Income, % of FPL") ytitle("# of New Enrollees") note("* Medicaid Eligibility at 138% FPL" "Bins of 5% of FPL") legend(off)
graph export ../output/main_results/figures/rd_5pct_n.pdf, replace

* Premium RD Graph
twoway (scatter premium bin_5, mcolor(gs8))(lfit premium bin_5 if bin_5>=150 & bin_5<200, lcolor(black))(lfit premium bin_5 if bin_5>=200 & bin_5<250, lcolor(black)) (lfit premium bin_5 if bin_5>=250 & bin_5<300, lcolor(black)) (lfit premium bin_5 if bin_5>=300 & bin_5<350, lcolor(black)) (lfit premium bin_5 if bin_5>=350 & bin_5<400, lcolor(black)), xline(150, lcolo(gs6)) xline(200, lcolor(gs6)) xline(250, lcolor(gs6)) xline(300, lcolor(gs6)) xline(350, lcolor(gs6)) xline(138, lcolor(gs6) lpattern(dash)) ///
graphregion(color(white)) ///
xtitle("Income, % of FPL") ytitle("Premium") note("* Medicaid Eligibility at 138% FPL" "Bins of 5% of FPL") legend(off) plotregion(lstyle(none)) xscale(nofextend) yscale(nofextend) xlabel(138 "*" 150 "150" 200 "200" 250 "250" 300 "300" 350 "350") ylab(, angle(0))
graph export ../output/main_results/figures/premium_rd_5pct.pdf, replace
restore
end

program region_netpremiums
clear
set obs 300

gen fpl = _n

gen Tufts = 0
replace Tufts = 44 if fpl > 150.1 & fpl < 200.1
replace Tufts = 84 if fpl > 200.1 & fpl < 250.1
replace Tufts = 126 if fpl > 250.1 & fpl < 300.1

gen BMC = 18
replace BMC = 17 if fpl > 100.1 & fpl < 150.1
replace BMC = 61 if fpl > 150.1 & fpl < 200.1
replace BMC = 102 if fpl > 200.1 & fpl < 250.1
replace BMC = 144 if fpl > 250.1 & fpl < 300.1

gen Fallon = 79
replace Fallon = 77 if fpl > 100.1 & fpl < 150.1
replace Fallon = 121 if fpl > 150.1 & fpl < 200.1
replace Fallon = 162 if fpl > 200.1 & fpl < 250.1
replace Fallon = 205 if fpl > 250.1 & fpl < 300.1

gen NHP = 168
replace NHP = 163 if fpl > 100.1 & fpl < 150.1
replace NHP = 207 if fpl > 150.1 & fpl < 200.1
replace NHP = 248 if fpl > 200.1 & fpl < 250.1
replace NHP = 292 if fpl > 250.1 & fpl < 300.1

*keep if inlist(fpl, 1,50,51,100,101,150,151,200,201,250,251,300)
keep if inlist(fpl, 138,150,151,200,201,250,251,300)


set scheme s1color
graph set eps fontface "CMU Serif"
graph set ps fontface "CMU Serif"
graph set svg fontface "CMU Serif"
graph set window fontface "CMU Serif"

twoway (connected Tufts fpl, lcolor(black) msymbol(s) msize(*1.2) lcolor(black) mfcolor("122 81 149")) (connected BMC fpl, lcolor(black) msymbol(t) msize(*1.2) mlcolor(black) mfcolor("239 86 117")) (connected Fallon fpl, lcolor(black) msymbol(c) msize(*1.2) mlcolor(black) mfcolor("255 166 0")) (connected NHP fpl, lcolor(black) msymbol(d) msize(*1.2) mlcolor(black) mfcolor(black)), ///
graphregion(color(white)) bgcolor(white) legend(off) xtitle("Income (% of FPL)", size(*1.2)) ytitle("Monthly Premium", size(*1.2)) ylab( , angle(horizontal) format(%12.2gc)) plotregion(color(white)) text(124 305 "Tufts Direct" 142 305 "BMC" 203 305 "Fallon" 290 305 "AllWays" 278 305 "Health" 266 305 "Partners", placement(e) size(*0.9)) ///
xlabel(138 " " 150 "150" 200 "200" 250 "250" 300 "300" 350 "350", labsize(*1)) xline(138, lcolor(gs8) lpattern(dash))
graph export ../output/main_results/figures/regionC2_netpremiums.pdf, replace
end

program enrollment_source
use ../../../mahc_sl/data/stata/20191212/Mark_AsOfDate11_02_2019, clear


********************************************************************************
/* The very first thing I'll do is identify new, "out-of-sample" enrollments. 
   I ONLY need the enrollment file to identify these enrollments, so I'm not 
   going to append the eligibility file until the end. */ 
********************************************************************************

drop if PeriodId>201911

* Drop dental 
drop if (Carrier=="ALTUS" | Carrier=="DELTADENTAL" | Carrier=="Delta Dental" | Carrier == "Altus Dental")

* Should be zero
drop if UniqueId==""

*Drop dups 
bysort UniqueId PeriodId: gen dups_id=_n
count if dups_id > 1	 
drop if dups_id > 1 

local date_vars ReportDate EffectiveDate BenefitStartDate TermDate
	foreach var of local date_vars{	
	rename `var' `var'_date
	gen `var' = date(`var'_date, "YMD")
	format `var' %td
	drop `var'_date
	}

	
generate letter_mailed = . 
replace letter_mailed = mdy(7,10,2018) if ReportDate == (mdy(7,3,2018))
replace letter_mailed = mdy(7,31,2018) if ReportDate == (mdy(7,17,2018))
replace letter_mailed = mdy(8,7,2018) if ReportDate == (mdy(7,31,2018))
replace letter_mailed = mdy(8,22,2018) if ReportDate == (mdy(8,14,2018))
replace letter_mailed = mdy(9,5,2018) if ReportDate == (mdy(8,28,2018))
replace letter_mailed = mdy(9,18,2018) if ReportDate == (mdy(9,11,2018))
replace letter_mailed = mdy(3,25,2019) if ReportDate == (mdy(3,19,2019))
replace letter_mailed = mdy(4,8,2019) if ReportDate == (mdy(4,2,2019))
 
replace letter_mailed = mdy(4,22,2019) if ReportDate == (mdy(4,16,2019))
replace letter_mailed = mdy(5,6,2019) if ReportDate == (mdy(4,30,2019))
replace letter_mailed = mdy(5,20,2019) if ReportDate == (mdy(5,14,2019))
replace letter_mailed = mdy(6,3,2019) if ReportDate == (mdy(5,28,2019))
replace letter_mailed = mdy(6,17,2019) if ReportDate == (mdy(6,11,2019))
replace letter_mailed = mdy(7,1,2019) if ReportDate == (mdy(6,25,2019))

format letter_mailed %td
recast str36 UniqueId, force


 

tostring PeriodId, gen(PeriodId_string)
gen int PeriodId_year = real(substr(PeriodId_string,1,4))
gen int PeriodId_month = real(substr(PeriodId_string,5,2))
gen PeriodId_date = ym(PeriodId_year, PeriodId_month)
format PeriodId_date %tmMCY
egen long UniqueId_num = group(UniqueId)

tsset UniqueId_num PeriodId_date
tsspell, fcond(PeriodId_date!=PeriodId_date[_n-1]+1|(_n==1))

* Create a new enrollment date: The first day of the month where _seq == 1
gen new_enrollment_date = mdy(PeriodId_month, 1, PeriodId_year) if _seq == 1
format new_enrollment_date %tdMDY

* General inclusion criteria: Age, Income/plan type, subscriber-only

drop if AgeAt < 18
drop if AgeAt > 64

drop if FamilySize > 1
drop if IsSubscriber == 0

keep if (PlanType == "2A" | PlanType == "2B" | PlanType == "3A" | PlanType == "3B")

drop if fpl > 300 

keep if PeriodId>= 201806 & PeriodId< 201908

* Only need first sequence
keep if _seq == 1

count if ReportDate == . 


/* Now we need to make sure we're attending to whether someone was in the sample 
   AT SOME POINT IN TIME, but had an "out of sample" enrollment (ie: before they
   were included in the study or more than 90 days out from letter mailing) */

gen enrollminusreport = new_enrollment_date - ReportDate

gen in_sample = . 
replace in_sample = 1 if new_enrollment_date - letter_mailed <= 90
replace in_sample = 0 if new_enrollment_date - letter_mailed < 0  // enrollments BEFORE study
replace in_sample = 0 if new_enrollment_date - letter_mailed > 90



gen n_NOTinsample = 1 if in_sample == 0



append using  "/disk/eagedisk1/mahc_sl.work/Master_Formatted_Data/JPAL_Flatfiles/JPAL_Demographics_082019"

* Drop jpal duplicates 

drop jpal_duplicate 

gen n_INsample = 1 if elig_fpl!=. // This just identifies observations in the appende file, one per person  

replace n_INsample = 0 if elig_fpl == . // this gives a 0 for all enrollment file rows

gen chart_date = . // this is a date variable we'll use for all observations of interest

replace chart_date = new_enrollment_date if n_NOTinsample == 1

replace chart_date = elig_reportdate if n_INsample == 1

format chart_date %td

gen keep = (n_INsample == 1 | n_NOTinsample == 1)

replace keep = 0 if keep == .

tab new_enrollment_date letter_mailed if keep == 0

*keep if keep == 1
gen n_insample_enroll = (keep==0)
replace n_insample_enroll=0 if mi(n_insample_enroll)

* Standardize date
replace chart_date = mdy(7,9,2018) if (elig_reportdate == (mdy(7,2,2018))|elig_reportdate == mdy(7,16,2018)) & (n_INsample==1)
replace chart_date = mdy(8,6,2018) if (elig_reportdate == (mdy(7,30,2018))|elig_reportdate == (mdy(8,13,2018))) & (n_INsample==1)
replace chart_date = mdy(9,3,2018) if (elig_reportdate == (mdy(8,27,2018))|elig_reportdate == (mdy(9,10,2018))) & (n_INsample==1)
replace chart_date = mdy(3,25,2019) if (elig_reportdate == (mdy(3,18,2019))|elig_reportdate == (mdy(4,1,2019))) & (n_INsample==1)
replace chart_date = mdy(4,22,2019) if (elig_reportdate == (mdy(4,15,2019))|elig_reportdate == (mdy(4,29,2019))) & (n_INsample==1)
replace chart_date = mdy(5,21,2019) if (elig_reportdate == (mdy(5,13,2019))|elig_reportdate == (mdy(5,28,2019))) & (n_INsample==1)
replace chart_date = mdy(6,17,2019) if (elig_reportdate == (mdy(6,10,2019))|elig_reportdate == (mdy(6,24,2019))) & (n_INsample==1)

replace chart_date = mdy(7,9,2018) if (ReportDate == (mdy(7,3,2018))|ReportDate == mdy(7,17,2018)) & (n_insample_enroll==1)
replace chart_date = mdy(8,6,2018) if (ReportDate == (mdy(7,31,2018))|ReportDate == (mdy(8,14,2018))) & (n_insample_enroll==1)
replace chart_date = mdy(9,3,2018) if (ReportDate == (mdy(8,28,2018))|ReportDate == (mdy(9,11,2018))) & (n_insample_enroll==1)
replace chart_date = mdy(3,25,2019) if (ReportDate == (mdy(3,19,2019))|ReportDate == (mdy(4,2,2019))) & (n_insample_enroll==1)
replace chart_date = mdy(4,22,2019) if (ReportDate == (mdy(4,16,2019))|ReportDate == (mdy(4,30,2019))) & (n_insample_enroll==1)
replace chart_date = mdy(5,21,2019) if (ReportDate == (mdy(5,14,2019))|ReportDate == (mdy(5,28,2019))) & (n_insample_enroll==1)
replace chart_date = mdy(6,17,2019) if (ReportDate == (mdy(6,11,2019))|ReportDate == (mdy(6,25,2019))) & (n_insample_enroll==1)

 


preserve
collapse (sum) n_*, by(chart_date)


set scheme s1color
graph set eps fontface "CMU Serif"
graph set ps fontface "CMU Serif"
graph set svg fontface "CMU Serif"
graph set window fontface "CMU Serif"

twoway (connected n_INsample chart_date if n_INsample !=0,/* mcolor(maroon) lcolor(maroon)*/ lcolor(black) msymbol(s) msize(*1.2) lcolor(black) mfcolor("122 81 149")) (connected n_NOTinsample chart_date if n_NOTinsample !=0,/* mcolor(navy) lcolor(navy)*/ lcolor(black) msymbol(t) msize(*1.2) mlcolor(black) mfcolor("239 86 117")) (connected n_insample_enroll chart_date if n_insample_enroll!=0,/* mcolor(dkorange) lcolor(dkorange)*/ lcolor(black) msymbol(c) msize(*1.2) mlcolor(black) mfcolor("255 166 0")), ///
graphregion(color(white)) bgcolor(white) legend(cols(1) label(1 "# Included In-Sample, by Eligibility Wave") label(2 "# Enrolled Out-of-Sample, by New Coverage Start Date") label(3 "# Enrolled In-Sample, by Eligibility Wave") size(*0.8)) xtitle("Date", size(*1.2)) ytitle("Counts", size(*1.2)) ylab( , angle(horizontal) format(%12.2gc)) xlab(, format(%tdnn/dd/YY)) plotregion(color(white))
graph export ../output/main_results/figures/in_vs_out_enrollment.pdf, replace
restore

end

* Execute
*main
